package com.weight.system.set.web;

import com.weight.system.datagroupd.PO.DatagroupdInfo;
import com.weight.system.datagroupd.service.DatagroupdInfoService;
import com.weight.system.set.PO.SetInfo;
import com.weight.system.set.VO.SetInfoVO;
import com.weight.system.set.service.SetInfoService;

import cn.afterturn.easypoi.excel.annotation.Excel;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

import org.springframework.beans.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.weight.common.util.JsonResultUtil;
import com.weight.common.util.CommonUtils;
import com.weight.common.util.DateFormUtils;
import com.weight.common.model.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import com.weight.common.enums.ResultEnum;

/**
 * 设置管理表
 */
@RestController
@Slf4j
@RequestMapping("/setInfo")
public class SetInfoController {

	@Autowired
	private SetInfoService setInfoService;

	private String pattern = "yyyy-MM-dd";

	@Autowired
	private DatagroupdInfoService datagroupdInfoService;
	/**
	 * 1.查询设置管理表--列表页 
	 * @return
	 */
	@RequestMapping(value="/queryForSetInfoList",method=RequestMethod.GET)
	public ModelAndView queryForSetInfoList(){

		Map<Object, Object> map =new HashMap<>();
		map.put("setStatus", 1); 
		
		//2、查询设置管理表列表
		List<SetInfo> setInfoList = setInfoService.findSetInfoByT(map);
		log.info("setInfoList={}",setInfoList);
		List<SetInfoVO> setInfoVOList = setInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
		//3、返回前台
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.setViewName("set/queryForSetInfoList");
		modelAndView.addObject("setInfoVOList", setInfoVOList);
		return modelAndView;
	}
	 
		@RequestMapping(value="/addSetInfo")
		public ModelAndView addSetInfo2(@RequestParam(value="setId",required=false) String setId){
			ModelAndView modelAndView = new ModelAndView();	
			modelAndView.setViewName("set/addSetInfo");
			Map<Object, Object> map =new HashMap<>();
			map.put("dataStatus", 1);
			map.put("datatabEname", "SetInfo");
			
			//2、查询数据结构表列表
			List<DatagroupdInfo> datagroupdInfoList = datagroupdInfoService.findDatagroupdInfoByT(map);
			
//			 datatabEname SetInfo
			
//			List<SetInfo> setInfoList = new ArrayList<SetInfo>();
//			SetInfo setInfo = new SetInfo();
//			DatagroupdInfo datagroupdInfo = null;
//			for(int i= 0 ;i<datagroupdInfoList.size();i++) {
//				datagroupdInfo = datagroupdInfoList.get(i);
//				setInfo = new SetInfo();
//				setInfo.setSetName(datagroupdInfo.getDataCname());
//				setInfo.setSetUrl(datagroupdInfo.getDataEname());
// 
//				setInfoList.add(setInfo);
//			}
			 
			modelAndView.addObject("datagroupdInfoList",datagroupdInfoList);
			
			return modelAndView;
		}
	
	 
	/**
	 * 5.保存 添加、修改设置管理表操作
	 * @param setInfoVO
	 * @param bindingResult
	 * @return
	 */
	@Transactional
	@RequestMapping(value="/saveSetInfo")
	public JsonResultUtil saveSetInfo(@Valid SetInfoVO setInfoVO,BindingResult bindingResult){
			String operateStr="add";
			//1.校验入参规则
			CommonUtils.validateBindingResult(bindingResult,setInfoVO);
			
			//2.获取id
			String setId=setInfoVO.getSetId();
			SetInfo setInfo=new SetInfo();
			BeanUtils.copyProperties(setInfoVO,setInfo);
			this.changeVOToPO(setInfoVO,setInfo);
			
			//3.判定id不为空，则为修改操作
			if(StringUtils.isNotBlank(setId)) {
				operateStr="edit";
				Map<String, String> map=new HashMap<>();
				map.put("setId", setId);
				log.info("修改操作，查询ID：setId={}",setId);
				//查询是否存在
				setInfoService.findBySetInfoId(map);
				try{
					setInfo.setUpdateBy("000");
					setInfoService.updateSetInfo(setInfo);
					log.info("更新操作成功：setInfo={}",setInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}else {//判定id为空，则为添加操作
				try{
					setInfo.setSetId(CommonUtils.getUUId());
					setInfo.setCreateBy("000");
					setInfo.setUpdateBy("000");
					setInfo.setSetStatus(1);
					setInfoService.insertSetInfo(setInfo);
					log.info("添加操作成功：setInfo={}",setInfo);
				}catch(Exception e){
					e.printStackTrace();
					log.error(e.getMessage(), e);
					return new JsonResultUtil(ResultEnum.EXCEPTION, "失败！", e.getMessage());
				}
			}
			return new JsonResultUtil(ResultEnum.SUCCESS, "操作成功！", operateStr);
	}

	/**
	 * 2.查询设置管理表--数据刷新  
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/queryForSetInfoListDG",method=RequestMethod.GET)
	public String queryForSetInfoListDG(HttpServletRequest request){
			//1、获取查询条件
			Integer page = Integer.parseInt(request.getParameter("page"));
			Integer rows = Integer.parseInt(request.getParameter("rows"));
			
			String setName = request.getParameter("setName");
			String setUrl = request.getParameter("setUrl");
			String setStatus = request.getParameter("setStatus");
			String setSort = request.getParameter("setSort");
			String setPic = request.getParameter("setPic");

			Map<Object, Object> map =new HashMap<>();
			map.put("setName", setName);
			map.put("setUrl", setUrl);
			map.put("setStatus", setStatus);
			map.put("setSort", setSort);
			map.put("setPic", setPic);

			
			//2、查询设置管理表列表
			List<SetInfo> setInfoList = setInfoService.findSetInfoByPage(page,rows,map);
			log.info("setInfoList={}",setInfoList);
			List<SetInfoVO> setInfoVOList = setInfoList.stream().map(e -> changePoToVO(e)).collect(Collectors.toList());
			//3、返回前台
			PageResult<SetInfoVO> result = new PageResult<SetInfoVO>(setInfoVOList);
			return  JSON.toJSONString(result);
	}

	/**
	 * 展示模板--转--保存模板  
	 * @param setInfoVO
	 * @param setInfo
	 * @return
	 */
	public void changeVOToPO(SetInfoVO setInfoVO,SetInfo setInfo){
			BeanUtils.copyProperties(setInfoVO,setInfo);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param setInfoVO
	 * @param setInfo
	 * @return
	 */
	public void changePoToVO(SetInfoVO setInfoVO,SetInfo setInfo){
			BeanUtils.copyProperties(setInfo,setInfoVO);
			

	}

	/**
	 * 保存模板--转--展示模板  
	 * @param setInfo
	 * @return
	 */
	public SetInfoVO changePoToVO(SetInfo setInfo){
			SetInfoVO setInfoVO=new SetInfoVO();
			BeanUtils.copyProperties(setInfo,setInfoVO);
			

			return setInfoVO;
	}

	/**
	 * 3.查询设置管理表--详情页
	 * @param setId
	 * @return
	 */
	@RequestMapping("/queryForSetInfoDetail")
	public ModelAndView queryForSetInfoDetail(@RequestParam(value="setId",required=false) String setId){
			//1.校验id
			CommonUtils.validateExistID(setId);
			
			//2.传参，查询
			Map<String, String> map=new HashMap<>();
			map.put("setId", setId);
			log.info("查询设置管理表入参，map={}",map);
			ModelAndView modelAndView = new ModelAndView();
			SetInfo setInfo = setInfoService.findBySetInfoId(map);
			
			SetInfoVO setInfoVO = changePoToVO(setInfo);
			//3、返回前台
			log.info("查询设置管理表查询成功：setInfo={}",setInfo);
			modelAndView.setViewName("system/set/queryForSetInfoDetail");
			modelAndView.addObject("setInfoVO",setInfoVO);
			modelAndView.addObject("formID",CommonUtils.getUUId());
			return modelAndView;
	}

	
	/**
	 * 6.删除设置管理表操作
	 * @param setId
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteBySetInfoId")
	public JsonResultUtil deleteBySetInfoId(@RequestParam(value="setId",required=false) String setId){
			//1.校验id
			CommonUtils.validateExistID(setId);
			
			//2.校验数据库中是否存在!
			Map<String, String> map=new HashMap<>();
			map.put("setId", setId);
			setInfoService.findBySetInfoId(map);
			//3.传参,添加操作人 删除
			map.put("updateBy","000");
			log.info("删除设置管理表入参，map={}",map);
			
			try{
				setInfoService.upDelBySetInfoId(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

	/**
	 * 6.删除设置管理表操作---批量删除
	 * @param setIdList
	 * @return
	 */
	@Transactional
	@RequestMapping("/deleteBySetInfoIdBatch")
	public JsonResultUtil deleteBySetInfoIdBatch(@RequestParam(value="setIdList",required=false) List setIdList){
			//1.校验id
			CommonUtils.validateExistIDList(setIdList);
			
			//2.校验数据库
			setInfoService.findBySetInfoIdList(setIdList);
			//3.传参 
			Map<String, Object> map=new HashMap<>();
			map.put("setIdList", setIdList);
			map.put("updateBy","000");
			log.info("批量删除设置管理表操作入参，map={}",map);
			
			try{
				setInfoService.upDelBySetInfoIdBatch(map);
				return new JsonResultUtil(ResultEnum.SUCCESS, "批量删除成功！", null);  
			}catch(Exception e){
				e.printStackTrace();
				log.error(e.getMessage(), e);
				return new JsonResultUtil(ResultEnum.EXCEPTION, "操作失败！", e.getMessage());
			}
	}

}